#--------------------------------------------
# VisualK-OS                                 
#    Module: Thor Config Screen class [v1.2] 
#    Author: Kevin Slaughter                 
#--------------------------------------------

"""
[Revision History]
    v1.0: Initial implementation... Having the screens encapsulated by
            the ScreenManager class with if-then-else blocks worked at
            first, when not much as required of the program, but more
            complex classes arose out of necessity to shrink/simplify
            the growing source code.
"""

import globals, curses, time
from VKCC_Constants import *


class cScreenCfgThor(object):
    ### General properties
    color = 2                       	# Color theme used within this screen
    title = "Configuration (Thor)"  	# Title to be displayed
    hasExit = True                  	# Flag: Display exit option
    
    ### Menu-specific properties
    curItem = 0                       		# Current menu item index
    numItems = (globals.Thor.numLights+1)	# Number of triggers/lights
    bColors = False                 		# Sets focus to color table
    COLUMN_1 = 4		# X-positions of columns
    COLUMN_2 = 20		# ^         ^          ^
    
    
    ### drawIndicator(): Draw cursor beside the current menu item
    def drawIndicator(self):
	iY = (5+(self.curItem/2))			# Determine row
	if (self.bColors): iY += 7			# ^--> Account for focus area
	if (self.curItem%2==0): iX = self.COLUMN_1	# Determine column
	else: iX = self.COLUMN_2
	globals.SM.printText(iX,iY,">>",T_HIGHLIGHT)	
	return
    
    
    ### fillTable(sOp): Populates the tables as needed
    def fillTable(self, scrn, sOp):       
        # Fill in data
        for t in range(0,self.numItems):
            # Plot
            iX = self.COLUMN_1 if (t%2==0) else self.COLUMN_2; iY = (5+(t/2))
            
            # Blank area to clea residual text
            scrn.printText((iX+4),(iY+7),"   ",T_NORMAL)
            
            # Display current thresholds
	    if (sOp == "CURRENT"):
            	scrn.printText((iX+2),iY, "    ", T_NORMAL)
            	scrn.printText((iX+2),iY, str(globals.Thor.thresholds[t]), T_NORMAL)
            	scrn.screen.refresh();
            
	    elif (sOp == "RT"):
	    	scrn.printText((iX+7),iY, str(globals.Thor.rtLevels[t]), T_NORMAL)
	    
            elif (sOp == "COLORS"):
            	# Display current colors
            	for t in range(0,8):
            	    iX = (self.COLUMN_1+3) if (t%2==0) else (self.COLUMN_2+3); iY = (12+(t/2));
            	    scrn.printText(iX,iY,"   ",(CB_BLUE+globals.Thor.colors[t]-1))        
        return
    
    
    ### handleKey(): Handle any passed keyboard input independent of GP usage.
    def handleKey(self, c, scr):
        # Check for and process any input
	if (c!=curses.ERR):	    
	    ### Input focus: Threshold level adjustment
	    if (not self.bColors):
		### Up arrow
		if ((c == curses.KEY_UP) | (c == KEY_8)):
		    # Normal operation
		    if (self.curItem > 0): self.curItem -= 1       # Previous item in list
		
		### Down arrow
		elif ((c == curses.KEY_DOWN) | (c == KEY_2)):
		    if (self.curItem < (self.numItems-1)): self.curItem += 1	# Next item in list
		    else:
			self.curItem = 0
			self.bColors = True	# Change input focus to color selection
		
		### Right arrow
		elif ((c == curses.KEY_RIGHT) | (c == KEY_6)):	# Increase current trigger threshold
		    if (globals.Thor.thresholds[self.curItem] < 1024):
			newVal = (globals.Thor.thresholds[self.curItem] + 16)
			globals.Thor.cfgThreshold(self.curItem,newVal)
			globals.Thor.updateThreshold(self.curItem)	# Send to Thor
		
		### Left arrow
		elif ((c == curses.KEY_LEFT) | (c == KEY_4)):	# Decrease current trigger threshold
		    if (globals.Thor.thresholds[self.curItem] > 16):
			newVal = (globals.Thor.thresholds[self.curItem] - 16)
			globals.Thor.cfgThreshold(self.curItem,newVal)
			globals.Thor.updateThreshold(self.curItem)	# Send to Thor
	    
	    ### Input focus: Color selection
	    else:
		### Up arrow
		if ((c == curses.KEY_UP) | (c == KEY_8)):
		    if (self.curItem > 0): self.curItem -= 1
		    else:
			self.bColors=0		# Return to threshold settings...
			self.curItem = (self.numItems-1)
		
		### Down arrow
		elif ((c == curses.KEY_DOWN) | (c == KEY_2)):
		    if (self.curItem < (self.numItems-1)): self.curItem += 1
		
		### Right arrow
		elif ((c == curses.KEY_RIGHT) | (c == KEY_6)):	# Increase current trigger threshold
		    if (globals.Thor.colors[self.curItem] < (VKC_MAX-2)):	# VKC_WHITE_RGB is not a defined curses color
			globals.Thor.colors[self.curItem] = (globals.Thor.colors[self.curItem]+1);
			globals.Thor.updateColor(self.curItem)	# Send to Thor
		
		### Left arrow
		elif ((c == curses.KEY_LEFT) | (c == KEY_4)):	# Decrease current trigger threshold
		    if (globals.Thor.colors[self.curItem] > 1):
			globals.Thor.colors[self.curItem] = (globals.Thor.colors[self.curItem]-1);
			globals.Thor.updateColor(self.curItem)	# Send to Thor
	    
	    # Show any changes
	    globals.SM.redraw = True
	    
	    # Key: Zero (Back to main cfg menu)
	    if (c == KEY_0): scr.changeScreen(SCREEN_CFG_MAIN)
        return
    
    
    ### paint(): Draw any elements used on this screen.
    def paint(self, scr):
        scr.menuPrep(self)
        
        # If Thor is not connected (or being emulated), this menu is moot.
        if (globals.Thor.isAvailable == False):
	    bKeyLoop = True
            scr.printText(2,3,"<Thor is not connected>",T_ERROR)
            return
        else:
            # Draw and populate tables
            scr.drawTable("THOR",2,5)
            self.fillTable(scr,"CURRENT")	# Populate current thresholds
            self.fillTable(scr,"RT")		# Populate R/T values
	    self.fillTable(scr,"COLORS")	# Display current colors of lights
            self.drawIndicator()
        return
    